<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Zabbix," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;最近在搞zabbix监控docker以及docker内部应用状态信息，网上找的资料好少，只找到了一个大神的一篇文章，用的是python实现监控docker容器的基本状态，在他给的脚本基础上进行修改，另外，增加了docker内部常用应用的状态监控，目前在测试环境上部署成功了，具体还需要在线上环境部署后才能">
<meta name="keywords" content="Zabbix">
<meta property="og:type" content="article">
<meta property="og:title" content="zabbix自动发现实现批量监控docker状态">
<meta property="og:url" content="https://hcldirgit.github.io/2017/09/03/Zabbix/26. zabbix自动发现实现批量监控docker状态/index.html">
<meta property="og:site_name" content="失落的乐章">
<meta property="og:description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;最近在搞zabbix监控docker以及docker内部应用状态信息，网上找的资料好少，只找到了一个大神的一篇文章，用的是python实现监控docker容器的基本状态，在他给的脚本基础上进行修改，另外，增加了docker内部常用应用的状态监控，目前在测试环境上部署成功了，具体还需要在线上环境部署后才能">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/zabbix%E8%87%AA%E5%8A%A8%E5%8F%91%E7%8E%B0%E5%AE%9E%E7%8E%B0%E6%89%B9%E9%87%8F%E7%9B%91%E6%8E%A7docker%E7%8A%B6%E6%80%81/01.jpeg?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/zabbix%E8%87%AA%E5%8A%A8%E5%8F%91%E7%8E%B0%E5%AE%9E%E7%8E%B0%E6%89%B9%E9%87%8F%E7%9B%91%E6%8E%A7docker%E7%8A%B6%E6%80%81/02.jpeg?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/zabbix%E8%87%AA%E5%8A%A8%E5%8F%91%E7%8E%B0%E5%AE%9E%E7%8E%B0%E6%89%B9%E9%87%8F%E7%9B%91%E6%8E%A7docker%E7%8A%B6%E6%80%81/03.jpeg?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/zabbix%E8%87%AA%E5%8A%A8%E5%8F%91%E7%8E%B0%E5%AE%9E%E7%8E%B0%E6%89%B9%E9%87%8F%E7%9B%91%E6%8E%A7docker%E7%8A%B6%E6%80%81/04.jpeg?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/zabbix%E8%87%AA%E5%8A%A8%E5%8F%91%E7%8E%B0%E5%AE%9E%E7%8E%B0%E6%89%B9%E9%87%8F%E7%9B%91%E6%8E%A7docker%E7%8A%B6%E6%80%81/05.jpeg?raw=true">
<meta property="og:updated_time" content="2017-09-02T17:39:54.557Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="zabbix自动发现实现批量监控docker状态">
<meta name="twitter:description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;最近在搞zabbix监控docker以及docker内部应用状态信息，网上找的资料好少，只找到了一个大神的一篇文章，用的是python实现监控docker容器的基本状态，在他给的脚本基础上进行修改，另外，增加了docker内部常用应用的状态监控，目前在测试环境上部署成功了，具体还需要在线上环境部署后才能">
<meta name="twitter:image" content="https://github.com/hcldirgit/image/blob/master/zabbix%E8%87%AA%E5%8A%A8%E5%8F%91%E7%8E%B0%E5%AE%9E%E7%8E%B0%E6%89%B9%E9%87%8F%E7%9B%91%E6%8E%A7docker%E7%8A%B6%E6%80%81/01.jpeg?raw=true">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://hcldirgit.github.io/2017/09/03/Zabbix/26. zabbix自动发现实现批量监控docker状态/"/>





  <title>zabbix自动发现实现批量监控docker状态 | 失落的乐章</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', '85*****1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?87980c**************99ec5e26fb5";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>











  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">失落的乐章</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">技术面前，永远都是学生。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-external-link"></i> <br />
            
            留言
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://hcldirgit.github.io/2017/09/03/Zabbix/26. zabbix自动发现实现批量监控docker状态/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="失落的乐章">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/0.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="失落的乐章">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">zabbix自动发现实现批量监控docker状态</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-03T02:13:44+08:00">
                2017-09-03
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最近在搞zabbix监控docker以及docker内部应用状态信息，网上找的资料好少，只找到了一个大神的一篇文章，用的是python实现监控docker容器的基本状态，在他给的脚本基础上进行修改，另外，增加了docker内部常用应用的状态监控，目前在测试环境上部署成功了，具体还需要在线上环境部署后才能检验出效果如何。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先，介绍下该监控套件有三个脚本，一个是自动发现主机上的docker容器脚本，另一个是用python写的获取每个docker容器的系统状态，包括CPU使用率，内存使用率以及网络资源使用率，最后这个脚本添加了一些我公司常用的应用的状态监控，包括应用占用内存，cpu资源以及进程的存活状态，至于读者们需要监控其他docker里面的应用，可以依照我的脚本来进行修改。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先，需要编辑自动发现docker中容器个数的脚本，内容如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line">cat /usr/<span class="built_in">local</span>/zabbix/scripts/docker_low_discovery.sh </div><div class="line"></div><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">#Fucation:docker low-level discovery</span></div><div class="line"><span class="function"><span class="title">docker</span></span>() &#123;</div><div class="line"> port=($(sudo docker ps -a|grep -v <span class="string">"CONTAINER ID"</span>|awk <span class="string">'&#123;print $NF&#125;'</span>))</div><div class="line"> <span class="built_in">printf</span> <span class="string">'&#123;\n'</span></div><div class="line"> <span class="built_in">printf</span> <span class="string">'\t"data":[\n'</span></div><div class="line"> <span class="keyword">for</span> key <span class="keyword">in</span> <span class="variable">$&#123;!port[@]&#125;</span></div><div class="line"> <span class="keyword">do</span></div><div class="line"> <span class="keyword">if</span> [[ <span class="string">"<span class="variable">$&#123;#port[@]&#125;</span>"</span> -gt 1 &amp;&amp; <span class="string">"<span class="variable">$&#123;key&#125;</span>"</span> -ne <span class="string">"<span class="variable">$(($&#123;#port[@]&#125;-1)</span>)"</span> ]];<span class="keyword">then</span></div><div class="line"> <span class="built_in">printf</span> <span class="string">'\t &#123;\n'</span></div><div class="line"> <span class="built_in">printf</span> <span class="string">"\t\t\t\"&#123;#CONTAINERNAME&#125;\":\"<span class="variable">$&#123;port[$&#123;key&#125;</span>]&#125;\"&#125;,\n"</span></div><div class="line">  </div><div class="line"> <span class="keyword">else</span> [[ <span class="string">"<span class="variable">$&#123;key&#125;</span>"</span> -eq <span class="string">"((<span class="variable">$&#123;#port[@]&#125;</span>-1))"</span> ]]</div><div class="line"> <span class="built_in">printf</span> <span class="string">'\t &#123;\n'</span></div><div class="line"> <span class="built_in">printf</span> <span class="string">"\t\t\t\"&#123;#CONTAINERNAME&#125;\":\"<span class="variable">$&#123;port[$&#123;key&#125;</span>]&#125;\"&#125;\n"</span></div><div class="line">  </div><div class="line"> <span class="keyword">fi</span></div><div class="line"> <span class="keyword">done</span></div><div class="line">  </div><div class="line"> <span class="built_in">printf</span> <span class="string">'\t ]\n'</span></div><div class="line"> <span class="built_in">printf</span> <span class="string">'&#125;\n'</span></div><div class="line">&#125;</div><div class="line"><span class="keyword">case</span> <span class="variable">$1</span> <span class="keyword">in</span></div><div class="line">docker)</div><div class="line">docker</div><div class="line">;;</div><div class="line">*)</div><div class="line"><span class="built_in">echo</span> <span class="string">"Usage:`basename <span class="variable">$0</span>` &#123;docker&#125;"</span></div><div class="line">;;</div><div class="line"><span class="keyword">esac</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面这个脚本是用来获取到docker里面应用的容器，并对其进行json化输出的，效果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">sh /usr/<span class="built_in">local</span>/zabbix/scripts/docker_low_discovery.sh docker</div><div class="line">&#123;</div><div class="line"><span class="string">"data"</span>:[</div><div class="line"> &#123;</div><div class="line"><span class="string">"&#123;#CONTAINERNAME&#125;"</span>:<span class="string">"hopeful_brown"</span>&#125;,</div><div class="line"> &#123;</div><div class="line"><span class="string">"&#123;#CONTAINERNAME&#125;"</span>:<span class="string">"happy_einstein"</span>&#125;</div><div class="line"> ]</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样就能被zabbix_server获取到了，然后是python脚本,使用python获取docker的参数需要使用一个扩展包，可以通过pip或者easy_install安装docker-py扩展：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;pip install docker-py或者easy_install docker-py或者不想这样安装的话可以去    python官网下载docker-py的安装包，解压后使用docker docker-py-1.4.0/setup.py     install命令安装，扩展包我将会打包放在附件中。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面是python脚本：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div></pre></td><td class="code"><pre><div class="line">cat /usr/<span class="built_in">local</span>/zabbix/scripts/docker_monitor.py </div><div class="line"><span class="comment">#!/usr/bin/env python</span></div><div class="line"><span class="comment">#-*- coding: utf-8 -*-</span></div><div class="line"><span class="comment">#author:Xianglin Hu</span></div><div class="line"><span class="comment">#email: a714585725@qq.com</span></div><div class="line">from docker import Client</div><div class="line">import sys</div><div class="line">import subprocess</div><div class="line">import os</div><div class="line"> </div><div class="line">def check_container_stats(container_name,collect_item):</div><div class="line"> container_collect=docker_client.stats(container_name)</div><div class="line"> old_result=<span class="built_in">eval</span>(container_collect.next())</div><div class="line"> new_result=<span class="built_in">eval</span>(container_collect.next())</div><div class="line"> container_collect.close()</div><div class="line"> <span class="keyword">if</span> collect_item == <span class="string">'cpu_total_usage'</span>:</div><div class="line"> result=new_result[<span class="string">'cpu_stats'</span>][<span class="string">'cpu_usage'</span>][<span class="string">'total_usage'</span>] - old_result[<span class="string">'cpu_stats'</span>][<span class="string">'cpu_usage'</span>][<span class="string">'total_usage'</span>]</div><div class="line"> <span class="keyword">elif</span> collect_item == <span class="string">'cpu_system_usage'</span>:</div><div class="line"> result=new_result[<span class="string">'cpu_stats'</span>][<span class="string">'system_cpu_usage'</span>] - old_result[<span class="string">'cpu_stats'</span>][<span class="string">'system_cpu_usage'</span>]</div><div class="line"> <span class="keyword">elif</span> collect_item == <span class="string">'cpu_percent'</span>:</div><div class="line"> cpu_total_usage=new_result[<span class="string">'cpu_stats'</span>][<span class="string">'cpu_usage'</span>][<span class="string">'total_usage'</span>] - old_result[<span class="string">'cpu_stats'</span>][<span class="string">'cpu_usage'</span>][<span class="string">'total_usage'</span>]</div><div class="line"> cpu_system_uasge=new_result[<span class="string">'cpu_stats'</span>][<span class="string">'system_cpu_usage'</span>] - old_result[<span class="string">'cpu_stats'</span>][<span class="string">'system_cpu_usage'</span>]</div><div class="line"> cpu_num=len(old_result[<span class="string">'cpu_stats'</span>][<span class="string">'cpu_usage'</span>][<span class="string">'percpu_usage'</span>])</div><div class="line"> result=round((<span class="built_in">float</span>(cpu_total_usage)/<span class="built_in">float</span>(cpu_system_uasge))*cpu_num*100.0,2)</div><div class="line"> <span class="keyword">elif</span> collect_item == <span class="string">'mem_usage'</span>:</div><div class="line"> result=new_result[<span class="string">'memory_stats'</span>][<span class="string">'usage'</span>]</div><div class="line"> <span class="keyword">elif</span> collect_item == <span class="string">'mem_limit'</span>:</div><div class="line"> result=new_result[<span class="string">'memory_stats'</span>][<span class="string">'limit'</span>]</div><div class="line"> <span class="keyword">elif</span> collect_item == <span class="string">'mem_percent'</span>:</div><div class="line"> mem_usage=new_result[<span class="string">'memory_stats'</span>][<span class="string">'usage'</span>]</div><div class="line"> mem_limit=new_result[<span class="string">'memory_stats'</span>][<span class="string">'limit'</span>]</div><div class="line"> result=round(<span class="built_in">float</span>(mem_usage)/<span class="built_in">float</span>(mem_limit)*100.0,2)</div><div class="line"> <span class="keyword">elif</span> collect_item == <span class="string">'network_rx_bytes'</span>:</div><div class="line"> network_check_command=<span class="string">""</span><span class="string">"sudo /usr/bin/docker exec %s cat /proc/net/dev|sed -n 3p|awk '&#123;print <span class="variable">$2</span>,<span class="variable">$10</span>&#125;'"</span><span class="string">""</span>%container_name</div><div class="line"> result=os.popen(network_check_command).<span class="built_in">read</span>().split()[0]</div><div class="line"> <span class="keyword">elif</span> collect_item == <span class="string">'network_tx_bytes'</span>:</div><div class="line"> network_check_command=<span class="string">""</span><span class="string">"sudo /usr/bin/docker exec %s cat /proc/net/dev|sed -n 3p|awk '&#123;print <span class="variable">$2</span>,<span class="variable">$10</span>&#125;'"</span><span class="string">""</span>%container_name</div><div class="line"> result=os.popen(network_check_command).<span class="built_in">read</span>().split()[1]</div><div class="line"> <span class="built_in">return</span> result</div><div class="line"><span class="keyword">if</span> __name__ == <span class="string">"__main__"</span>:</div><div class="line"> docker_client = Client(base_url=<span class="string">'unix://var/run/docker.sock'</span>, version=<span class="string">'1.17'</span>)</div><div class="line"> container_name=sys.argv[1]</div><div class="line"> collect_item=sys.argv[2]</div><div class="line"> <span class="built_in">print</span> check_container_stats(container_name,collect_item)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里面使用到了docker里面的Client类，获取到某个docker容器的当前状态信息，然后进行运算，返回运算结果。但是容器当前信息那个dict中的network的key获取到的信息不准确，于是我使用了docker exec命令来获取docker容器内部的网络流量信息。这也是在大神的基础上进行改进的地方，这里的改进将python脚本的执行时间缩短了，将有助于server获取duocker容器信息时减少长连接的数量，提升zabbix_server的性能。这里的docker exec命令将会在下一个脚本中大量使用来获取docker容器中的应用状态信息。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面是获取容器应用状态信息的脚本：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line">cat docker_processmonitor.sh </div><div class="line"></div><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">#license:GPL</span></div><div class="line"><span class="comment">#mail:a714585725@qq.com</span></div><div class="line"><span class="comment">#date:2015.09.22</span></div><div class="line"><span class="function"><span class="title">processmem</span></span>()&#123;</div><div class="line"> sudo /usr/bin/docker <span class="built_in">exec</span> <span class="variable">$1</span> ps aux|grep <span class="variable">$2</span>|grep -v <span class="string">"grep"</span>|grep -v <span class="string">"processstatus.sh"</span>|awk <span class="string">'&#123;sum+=$6&#125;; END&#123;print sum&#125;'</span></div><div class="line">&#125;</div><div class="line"><span class="function"><span class="title">processcpu</span></span>()&#123;</div><div class="line"> sudo /usr/bin/docker <span class="built_in">exec</span> <span class="variable">$1</span> ps aux|grep <span class="variable">$2</span>|grep -v <span class="string">"grep"</span>|grep -v <span class="string">"processstatus.sh"</span>|awk <span class="string">'&#123;sum+=$3&#125;; END&#123;print sum&#125;'</span></div><div class="line">&#125;</div><div class="line"><span class="function"><span class="title">processport</span></span>()&#123;</div><div class="line"> sudo /usr/bin/docker <span class="built_in">exec</span> <span class="variable">$1</span> ss -antlp|grep <span class="variable">$2</span>|grep LISTEN|wc -l</div><div class="line">&#125;</div><div class="line"><span class="keyword">case</span> <span class="string">"<span class="variable">$3</span>"</span> <span class="keyword">in</span></div><div class="line">mem)</div><div class="line">processmem <span class="variable">$1</span> <span class="variable">$2</span></div><div class="line">;;</div><div class="line">cpu)</div><div class="line">processcpu <span class="variable">$1</span> <span class="variable">$2</span></div><div class="line">;;</div><div class="line">port)</div><div class="line">processport <span class="variable">$1</span> <span class="variable">$2</span></div><div class="line">;;</div><div class="line">*)</div><div class="line"><span class="built_in">echo</span> <span class="string">"Usage: <span class="variable">$0</span> &#123;docker_containername&#125;&#123;processname&#125;&#123;mem|cpu|port&#125;"</span></div><div class="line">;;</div><div class="line"><span class="keyword">esac</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个脚本使用了一个case来判断需要获取的docker容器的名称以及该容器中应用的状态信息，只不过这里获取docker容器状态信息使用的是docker exec命令来进行获取。另外这里面添加了对于应用是否存活的状态监测，那就是检测该应用是否侦听了网络端口，假如该应用侦听的网络端口个数为0的话，可以认为该应用存在异常。可以根据自己需求修改相应应用的监控。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;由于docker是以root权限来启用的，而zabbix监控是使用zabbix用户来执行的，所以需要给予zabbix用户相应的权限，需要编辑visudo：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">echo</span> <span class="string">"zabbix ALL=(root) 	NOPASSWD:/bin/docker,/usr/bin/python,/usr/local/zabbix/scripts/docker_monitor.py,/usr/local/zabbix/scripts/docker_low_discovery.sh,/usr/local/zabbix/scripts/docker_processmonitor.sh"</span>&gt;&gt;/etc/sudoers</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;并且还需要注释掉这条记录：#Defaults    requiretty（PS：注意，这条记录是要求使用sudo命令时需要有终端界面，注释掉这一条之后就可以不需要终端执行sudo命令了。）</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后就是编辑zabbix_agentd.conf文件,添加下面几行：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">tail -3 /usr/<span class="built_in">local</span>/zabbix/etc/zabbix_agentd.conf</div><div class="line"></div><div class="line">UserParameter=docker_low_discovery[*],/bin/bash /usr/<span class="built_in">local</span>/zabbix/scripts/docker_low_discovery.sh <span class="variable">$1</span></div><div class="line">UserParameter=docker_stats[*],sudo /usr/bin/python /usr/<span class="built_in">local</span>/zabbix/scripts/docker_monitor.py <span class="variable">$1</span> <span class="variable">$2</span></div><div class="line">UserParameter=docker_process[*],/bin/bash /usr/<span class="built_in">local</span>/zabbix/scripts/docker_processmonitor.sh <span class="variable">$1</span> <span class="variable">$2</span> <span class="variable">$3</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存配置并重启zabbix_agentd服务，然后修改脚本的属主属组以及权限：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">chown zabbix.zabbix /usr/<span class="built_in">local</span>/zabbix/scripts/*</div><div class="line">chmod 755 /usr/<span class="built_in">local</span>/zabbix/scripts/*</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后可以在zabbix_server端测试是否能够获取到相应的数据：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">[root@test1 ~]<span class="comment"># /usr/local/zabbix-2.4.4/bin/zabbix_get -s x.x.x.x -k"docker_low_discovery[docker]"</span></div><div class="line">&#123;</div><div class="line"><span class="string">"data"</span>:[</div><div class="line"> &#123;</div><div class="line"><span class="string">"&#123;#CONTAINERNAME&#125;"</span>:<span class="string">"hopeful_brown"</span>&#125;,</div><div class="line"> &#123;</div><div class="line"><span class="string">"&#123;#CONTAINERNAME&#125;"</span>:<span class="string">"happy_einstein"</span>&#125;</div><div class="line"> ]</div><div class="line">&#125;</div><div class="line">[root@test1 ~]<span class="comment"># /usr/local/zabbix-2.4.4/bin/zabbix_get -s x.x.x.x -k"docker_stats[happy_einstein,network_tx_bytes]"</span></div><div class="line">9664252</div><div class="line">[root@test1 ~]<span class="comment"># /usr/local/zabbix-2.4.4/bin/zabbix_get -s x.x.x.x -k"docker_process[happy_einstein,nginx,port]"</span></div><div class="line">2</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里的IP地址就用x.x.x.x代替了，这里应该填写客户端的IP地址。如上所示，能够正确获取到agentd的数据以后，然后就需要在zabbix_server这边配置监控模版了</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E8%87%AA%E5%8A%A8%E5%8F%91%E7%8E%B0%E5%AE%9E%E7%8E%B0%E6%89%B9%E9%87%8F%E7%9B%91%E6%8E%A7docker%E7%8A%B6%E6%80%81/01.jpeg?raw=true" alt=""></p>
<p><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E8%87%AA%E5%8A%A8%E5%8F%91%E7%8E%B0%E5%AE%9E%E7%8E%B0%E6%89%B9%E9%87%8F%E7%9B%91%E6%8E%A7docker%E7%8A%B6%E6%80%81/02.jpeg?raw=true" alt=""></p>
<p><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E8%87%AA%E5%8A%A8%E5%8F%91%E7%8E%B0%E5%AE%9E%E7%8E%B0%E6%89%B9%E9%87%8F%E7%9B%91%E6%8E%A7docker%E7%8A%B6%E6%80%81/03.jpeg?raw=true" alt=""></p>
<p><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E8%87%AA%E5%8A%A8%E5%8F%91%E7%8E%B0%E5%AE%9E%E7%8E%B0%E6%89%B9%E9%87%8F%E7%9B%91%E6%8E%A7docker%E7%8A%B6%E6%80%81/04.jpeg?raw=true" alt=""></p>
<p><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E8%87%AA%E5%8A%A8%E5%8F%91%E7%8E%B0%E5%AE%9E%E7%8E%B0%E6%89%B9%E9%87%8F%E7%9B%91%E6%8E%A7docker%E7%8A%B6%E6%80%81/05.jpeg?raw=true" alt=""></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面这些显示的是监控的结果，也可以根据自己的需求来进行修改</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Zabbix/" rel="tag"># Zabbix</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/03/Zabbix/25. zabbix通过自定义脚本监控nginx，php-fpm和mysql占用内存数和进程的个数/" rel="next" title="zabbix通过自定义脚本监控nginx，php-fpm和mysql占用内存数和进程的个数">
                <i class="fa fa-chevron-left"></i> zabbix通过自定义脚本监控nginx，php-fpm和mysql占用内存数和进程的个数
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/03/Zabbix/3. zabbix 增加客户端监控/" rel="prev" title="zabbix 增加客户端监控">
                zabbix 增加客户端监控 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      

      <section class="site-overview sidebar-panel sidebar-panel-active">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/0.png"
               alt="失落的乐章" />
          <p class="site-author-name" itemprop="name">失落的乐章</p>
           
              <p class="site-description motion-element" itemprop="description">失落的乐章的Blog</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/">
                <span class="site-state-item-count">627</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">38</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/hcldirgit" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">失落的乐章</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
